import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/mine_business/mine_following/mine_follow_sub_page.dart';
import 'package:xvideos/hj_page/mine_business/mine_following/widget/follow_list_items.dart';
import 'package:xvideos/hj_utils/api_service/common_service.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';

import '../../../hj_model/mine/follow_user_list_model.dart';

abstract class MineFollowSubLogic extends GetxController with GetTickerProviderStateMixin {
  final int? uid;
  MineFollowSubLogic(this.uid);

  RxInt loadCount = 0.obs;

  final dataSource = [];

  RefreshController? refreshController;
  int pageNumber = 1;
  bool isLoading = true;
  bool isGridStyle = false;

  @override
  void onReady() {
    super.onReady();
    fetchPageData();
  }

  @mustCallSuper
  fetchPageData({bool isRefresh = true}) {
    if (isRefresh) {
      pageNumber = 1;
    }
  }

  Widget instanceChildItem(int index);
}

class ActressController extends MineFollowSubLogic {
  ActressController(super.uid);

  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final retModel = await MineService.fetchMineCollectList<FollowUserModel>(
      'actress',
      page: pageNumber,
      size: 10,
      uid: uid,
    );

    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (retModel?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(retModel?.list ?? []);
    pageNumber += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return FollowActressItem(dataSource[index]);
  }
}

class BloggerController extends MineFollowSubLogic {
  BloggerController(super.uid);

  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final retModel = await CommonService.getFollowUsers(
      pageNumber: pageNumber,
      pageSize: 10,
      uid: uid,
    );

    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (retModel?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(retModel?.list ?? []);
    pageNumber += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return FollowBloggerItem(dataSource[index]);
  }
}

class TopicController extends MineFollowSubLogic {
  TopicController(super.uid);
  @override
  bool get isGridStyle => true;

  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final retModel = await MineService.fetchMineCollectList<TagsBean>("tag", page: pageNumber, size: 10, uid: uid);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (retModel?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(retModel?.list ?? []);
    pageNumber += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return FollowTopicItem(dataSource[index]);
  }
}

MineFollowSubLogic instanceController(FollowType type, {int? uid}) {
  switch (type) {
    case FollowType.user:
      return BloggerController(uid);
    case FollowType.topic:
      return TopicController(uid);
    case FollowType.actress:
      return ActressController(uid);
    default:
      throw 'type 没有定义';
  }
}
